﻿/*************************************************************************
 * 
 * Filename :flashconfig
 * 
 * steven hu   2010/3/15 8:22:51
 *  
 * http://www.cnblogs.com/huxj
 *   
 * 
 * Change History:
 * 
 * 
**************************************************************************/



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hxj.Common;
using Hxj.Web;
using System.IO;
using System.Text;

namespace RicciWebsite.admin
{
    public partial class flashconfig : RicciWebsite.Pages.AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getData();
            }
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        private void getData()
        {
            RicciWebsite.Utils.flashconfig config = RicciWebsite.Utils.flashconfigHelper.Load();

            txttime.Text = config.AutoPlayTime.ToString();

            RicciWebsite.Utils.flashconfigitem item = config.Items[0];

            txtsort1.Text = item.Id.ToString();
            txturl1.Text = item.Link;
            imgshow1.ImageUrl = "~/" + item.Item_url.TrimStart('/', '\\');

            item = config.Items[1];
            txtsort2.Text = item.Id.ToString();
            txturl2.Text = item.Link;
            imgshow2.ImageUrl = "~/" + item.Item_url.TrimStart('/', '\\');

            item = config.Items[2];
            txtsort3.Text = item.Id.ToString();
            txturl3.Text = item.Link;
            imgshow3.ImageUrl = "~/" + item.Item_url.TrimStart('/', '\\');

            item = config.Items[3];
            txtsort4.Text = item.Id.ToString();
            txturl4.Text = item.Link;
            imgshow4.ImageUrl = "~/" + item.Item_url.TrimStart('/', '\\');

        }


        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (!IsValid)
                return;

            RicciWebsite.Utils.flashconfig config = new RicciWebsite.Utils.flashconfig();
            config.AutoPlayTime = ConvertHelper.ToInt32(txttime.Text, 10);

            UploadFile upload = new UploadFile();
            upload.AddAllowFileExtension("jpg", "gif", "jpeg");
            upload.FilePath = "~/webpic/images";
            UploadFileInfo uploadfileinfo;
            string rootpath = Server.MapPath("~/");
            List<RicciWebsite.Utils.flashconfigitem> items = new List<RicciWebsite.Utils.flashconfigitem>();
            List<RicciWebsite.Utils.flashconfigitem> itemsold = RicciWebsite.Utils.flashconfigHelper.Load().Items;

            List<string> deletefiles = new List<string>();


            RicciWebsite.Utils.flashconfigitem item = new RicciWebsite.Utils.flashconfigitem();
            item.Id = Convert.ToInt32(txtsort1.Text);
            item.Link = txturl1.Text.Trim();
            if (fileimg1.HasFile)
            {
                uploadfileinfo = upload.Upload(fileimg1.PostedFile);

                if (uploadfileinfo.HasError)
                {
                    JavaScript.MsgBox(uploadfileinfo.ErrorText);
                    return;
                }
                else
                {
                    item.Item_url = uploadfileinfo.Path.Replace("~/", "");
                    deletefiles.Add(Path.Combine(rootpath, itemsold[0].Item_url));
                }
            }
            else
            {
                item.Item_url = itemsold[0].Item_url;
            }
            items.Add(item);


            item = new RicciWebsite.Utils.flashconfigitem();
            item.Id = Convert.ToInt32(txtsort2.Text);
            item.Link = txturl2.Text.Trim();
            if (fileimg2.HasFile)
            {
                uploadfileinfo = upload.Upload(fileimg2.PostedFile);

                if (uploadfileinfo.HasError)
                {
                    JavaScript.MsgBox(uploadfileinfo.ErrorText);
                    return;
                }
                else
                {
                    item.Item_url = uploadfileinfo.Path.Replace("~/", "");
                    deletefiles.Add(Path.Combine(rootpath, itemsold[1].Item_url));
                }


            }
            else
            {
                item.Item_url = itemsold[1].Item_url;
            }
            items.Add(item);


            item = new RicciWebsite.Utils.flashconfigitem();
            item.Id = Convert.ToInt32(txtsort3.Text);
            item.Link = txturl3.Text.Trim();
            if (fileimg3.HasFile)
            {
                uploadfileinfo = upload.Upload(fileimg3.PostedFile);


                if (uploadfileinfo.HasError)
                {
                    JavaScript.MsgBox(uploadfileinfo.ErrorText);
                    return;
                }
                else
                {
                    item.Item_url = uploadfileinfo.Path.Replace("~/", "");
                    deletefiles.Add(Path.Combine(rootpath, itemsold[2].Item_url));
                }

            }
            else
            {
                item.Item_url = itemsold[2].Item_url;
            }
            items.Add(item);

            item = new RicciWebsite.Utils.flashconfigitem();
            item.Id = Convert.ToInt32(txtsort4.Text);
            item.Link = txturl4.Text.Trim();
            if (fileimg4.HasFile)
            {
                uploadfileinfo = upload.Upload(fileimg4.PostedFile);


                if (uploadfileinfo.HasError)
                {
                    JavaScript.MsgBox(uploadfileinfo.ErrorText);
                    return;
                }
                else
                {
                    item.Item_url = uploadfileinfo.Path.Replace("~/", "");
                    deletefiles.Add(Path.Combine(rootpath, itemsold[3].Item_url));
                }
            }
            else
            {
                item.Item_url = itemsold[3].Item_url;
            }
            items.Add(item);


            items.Sort();

            config.Items = items;

            foreach (string deletef in deletefiles)
            {
                File.Delete(deletef);
            }

            RicciWebsite.Utils.flashconfigHelper.Set(config);


            JavaScript.MsgBox("操作成功!");

            getData();
        }
    }
}
